﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using WindowsGame1.gamePlugins.renderer;


namespace WindowsGame1.gamePlugins.powerups
{
    class Powerups
    {        
        //timer
        public int miliseconds;
        public int interval;
        public bool timeraan = false;
        public int miliseconds2;
        public int interval2;
        public bool timeraan2 = false;
        //achtergrond kleur aanpassen
        public int rood = 0;
        public int groen = 0;
        public int blauw = 0;
        public bool shouldWait = false;
        //random getal
        Random r = new Random();

        public bool[] pu = new bool[7];
        public bool aangeroepen = false;
        ContentManager newContent;
        static bool disco = false;
        Texture2D pongzwartTexture;
        Texture2D pongwitTexture;
        Texture2D blocksmileTexture;
        Texture2D blocksadTexture;
        

        /* hier moet alle content geladen worden */
        public Powerups(ContentManager Content)
        {
            newContent = Content;
            // dinges = Content.Load<Texture2D>("pongbalwit");
            // MAAR: dit is eigenlijk al eerder gedaan in de objectsx lijst
            pongzwartTexture = Content.Load<Texture2D>("pongbalzwart");
            pongwitTexture = Content.Load<Texture2D>("pongbalwit");
            blocksmileTexture = Content.Load<Texture2D>("blocksmile");
            blocksadTexture = Content.Load<Texture2D>("blocksad");
        }

        public List<PhysicsObject> initialize(List<PhysicsObject> objectsx)
        {
            // Alle powerups objecten kunnen hier worden aangemaakt
            objectsx.Add(new PhysicsObject { name = "pu_ballgrote", texture = newContent.Load<Texture2D>("pu_ballgrote"), speed = Vector2.Zero, position = new Vector2(500F, 350F), visible = false, solidCollision = false, scale = 1F });
            objectsx.Add(new PhysicsObject { name = "pu_block", texture = newContent.Load<Texture2D>("pu_block"), speed = Vector2.Zero, position = new Vector2(550F, 350F), visible = false, solidCollision = false, scale = 1F });
            objectsx.Add(new PhysicsObject { name = "pu_disco", texture = newContent.Load<Texture2D>("pu_disco"), speed = Vector2.Zero, position = new Vector2(600F, 350F), visible = false, solidCollision = false, scale = 1F });
            objectsx.Add(new PhysicsObject { name = "pu_zwartwit", texture = newContent.Load<Texture2D>("pu_zwartwit"), speed = Vector2.Zero, position = new Vector2(600F, 350F), visible = false, solidCollision = false, scale = 1F });
            objectsx.Add(new PhysicsObject { name = "blocksmile", texture = newContent.Load<Texture2D>("blocksmile"), speed = Vector2.Zero, position = new Vector2(500F, 350F), visible = false, solidCollision = false, scale = 1F});
            return objectsx;
        }

        /* Er is een botsing tussen obj en objx */
        public List<PhysicsObject> collision(List<PhysicsObject> objectsx, PhysicsObject obj, PhysicsObject objx)
        {

            if ((objx == objectsx[3]) && (objectsx[3].visible == true) && (objectsx[3].solidCollision == true))
                       {
                

                objectsx[3].visible = false;
                objectsx[3].solidCollision = false;

                objectsx[3].speed.Y = 0;

                pu[3] = true;
            }
                
            else if ((objx == objectsx[4]) && (objectsx[4].visible == true) && (objectsx[4].solidCollision == true))
            {
                
                 
                objectsx[4].visible = false;
                objectsx[4].solidCollision = false;

                objectsx[4].speed.Y = 0;

                pu[4] = true;
            }
            else if ((objx == objectsx[5]) && (objectsx[5].visible == true) && (objectsx[5].solidCollision == true))
            {
                

                objectsx[5].visible = false;
                objectsx[5].solidCollision = false;

                objectsx[5].speed.Y = 0;

                pu[5] = true;
            }
            else if ((objx == objectsx[6]) && (objectsx[6].visible == true) && (objectsx[6].solidCollision == true))
            {
                

                objectsx[6].visible = false;
                objectsx[6].solidCollision = false;

                objectsx[6].speed.Y = 0;

                pu[6] = true;
            }
            else if ((objx == objectsx[7]) && (objectsx[7].visible == true) && (objectsx[7].solidCollision == true))
            {
                obj.speed.X *= -1;
                objectsx[7].texture = blocksadTexture;
                timeraan2 = true;

            }

            return objectsx;


        }

        /* Deze code wordt elke frame uitgevoerd, 60x per seconde */
        public void update(Microsoft.Xna.Framework.GameTime gameTime, Physics engine, List<PhysicsObject> objectsx, int[] viewPort)
        {
            if (!shouldWait) {
            // random bepalen welke power up in moet verschijnen
                if ((!aangeroepen) && (pu[3] != true) && (pu[4] != true) && (pu[5] != true) && (pu[6] != true))
                {
                    int getal = r.Next(1, 350);
                    if ((getal == 2) && (objectsx[3].visible == false) && (objectsx[4].visible == false) && (objectsx[5].visible == false) && (objectsx[6].visible == false))
                    {
                        objectsx[3].visible = true;
                        objectsx[3].solidCollision = true;
                    }
                    if ((getal == 4) && (objectsx[3].visible == false) && (objectsx[4].visible == false) && (objectsx[5].visible == false) && (objectsx[6].visible == false))
                    {
                        objectsx[4].visible = true;
                        objectsx[4].solidCollision = true;
                    }
                    if ((getal == 6) && (objectsx[3].visible == false) && (objectsx[4].visible == false) && (objectsx[5].visible == false) && (objectsx[6].visible == false))
                    {
                        objectsx[5].visible = true;
                        objectsx[5].solidCollision = true;
                    }
                    if ((getal == 8) && (objectsx[3].visible == false) && (objectsx[4].visible == false) && (objectsx[5].visible == false) && (objectsx[6].visible == false))
                    {
                        objectsx[6].visible = true;
                        objectsx[6].solidCollision = true;
                    }



                    //checken welke power up visible is geworden en die een snelheid geven
                    if (objectsx[3].visible == true)
                    {
                        aangeroepen = true;
                        objectsx[3].speed.Y = 0.5F;
                    }
                    if (objectsx[4].visible == true)
                    {
                        aangeroepen = true;
                        objectsx[4].speed.Y = 0.5F;
                    }
                    if (objectsx[5].visible == true)
                    {
                        aangeroepen = true;
                        objectsx[5].speed.Y = -0.5F;
                    }
                    if (objectsx[6].visible == true)
                    {
                        aangeroepen = true;
                        objectsx[6].speed.Y = -0.5F;
                    }
                }
            }


            // als er een collisen met een power up is geweest dan wordt er een variable pu3 t/m pu6 op true gezet. als hij true is wordt de timer aangezet en wordt de powerup uitgevoerd.
            if (pu[3] == true)
            {
                if (timeraan == false)
                {
                    timeraan = true;
                }
                objectsx[0].scale = 0.1F;
            }
            if (pu[4] == true)
            {
                // block
                objectsx[7].visible = true;
                objectsx[7].solidCollision = true;
                int hoogtescherm = viewPort[1];
                int positieblock = (hoogtescherm / 2) - 150;
                int breedtescherm = viewPort[0];
                int positieblock2 = (breedtescherm / 2) - 40;
                objectsx[7].position = new Vector2(positieblock2,positieblock);
               

            }
            if (pu[5] == true)
            {
                if (timeraan == false)
                {
                    timeraan = true;
                }
                
                rood = r.Next(1, 256);
                groen = r.Next(1, 256);
                blauw = r.Next(1, 256);
                if (!disco)
                {
                    objectsx[0].speed = objectsx[0].speed * 2;
                    disco = true;
                }
            }
            if (pu[6] == true)
            {
                
                if (timeraan == false)
                {
                    timeraan = true;
                }

                if (objectsx[0].position.X > 960)
                {
                    
                    objectsx[0].texture = pongwitTexture;
                                      
                    
                }
                else if (objectsx[0].position.X < 320)
                {
                    objectsx[0].texture = pongwitTexture;
                    

                }
                else
                {
                    objectsx[0].texture = pongzwartTexture;
                   
                }


            }


            //timer
            if (timeraan == true)
            {
                miliseconds = 10000 + gameTime.ElapsedGameTime.Milliseconds;
                interval += gameTime.ElapsedGameTime.Milliseconds;
                if (interval > miliseconds)
                {


                    if (pu[3] == true)
                    {
                        objectsx[0].scale = 1F;
                        pu[3] = false;
                    }
                    
                    if (pu[5] == true)
                    {
                        rood = 0;
                        groen = 0;
                        blauw = 0;
                        objectsx[0].speed = objectsx[0].speed / 2;
                        disco = false;
                        pu[5] = false;
                    }
                    if (pu[6] == true)
                    {
                        objectsx[0].texture = pongwitTexture;
                        
                        pu[6] = false;
                    }
                    timeraan = false;
                    aangeroepen = false;
                    interval = 0;
                    miliseconds = 0;

                }
            }

            //block timer
            if (timeraan2 == true)
            {
                miliseconds2 = 1500 + gameTime.ElapsedGameTime.Milliseconds;
                interval2 += gameTime.ElapsedGameTime.Milliseconds;
                if (interval2 > miliseconds2)
                {
                    objectsx[7].texture = blocksmileTexture;
                    timeraan2 = false;
                    objectsx[7].visible = false;
                    objectsx[7].solidCollision = false;
                    pu[4] = false;
                    objectsx[7].texture = blocksmileTexture;
                    objectsx[7].layerDepth = 100;
                    timeraan2 = false;
                    interval2 = 0;
                    miliseconds2 = 0;
                }
                

            }








        }

    }
}
